class EmployeesController < ApplicationController
  
  def index
    params[:key_word] = "%#{params[:key_words]}%"
    @employees = Employee.where('full_name like :key_word or mobile like :key_word or address like :key_word',params).order('created_at asc').visible
  end
  
  def new
    @employee = Employee.new
  end
  
  def create
    @employee = Employee.new(params[:employee])
    if @employee.save
      @employee.reset_posts!( 
        params[:post_saler_in_dept_id], 
        params[:post_officer_in_dept_id],
        params[:post_installer_in_dept_id],
        params[:post_store_admin_in_dept_id]
      )
      redirect_to :action => :index
    else
      render :new
    end
  end
  
  def edit
    @employee = Employee.find(params[:id])
  end
  
  def update
    @employee = Employee.find(params[:id])
    if @employee.update_attributes(params[:employee])
      @employee.reset_posts!( 
        params[:post_saler_in_dept_id], 
        params[:post_officer_in_dept_id],
        params[:post_installer_in_dept_id],
        params[:post_store_admin_in_dept_id]
      )
      redirect_to :action => :index
    else
      render :edit
    end
  end
  
  def destroy
    @employee = Employee.find(params[:id])
    @employee.destroy
    redirect_to :action => :index
  end
  
  def hide
    @employee = Employee.find(params[:id])
    @employee.hidden!
    redirect_to :action => :index
  end

  def visible
    @employee = Employee.find(params[:id])
    @employee.visible!
    redirect_to :action => :index
  end
end
